import { Router } from "vue-router"
import { getToken } from "@/utils/setToken"

const whitePathList = ['/login']

export function createRouterGuards(router: Router) {
  const userToken = getToken()
  router.beforeEach(async (to, from, next) => {
    if (userToken) {
      if (to.path === '/login') {
        next({ path: '/' })
      } else {
        next()
      }
    } else {
      if (whitePathList.includes(to.path)) {
        next()
      } else {
        next({ path: '/login' })
      }
    }
  })
}